<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Chapter 40. Boost.Typeof</title>
<link rel="stylesheet" href="../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="libraries.html" title="Part I. The Boost C++ Libraries (BoostBook Subset)">
<link rel="prev" href="boost_typeindex/acknowledgements.html" title="Acknowledgements">
<link rel="next" href="typeof/tuto.html" title="Tutorial">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../boost.png"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_typeindex/acknowledgements.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="typeof/tuto.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="chapter">
<div class="titlepage"><div>
<div><h2 class="title">
<a name="typeof"></a>Chapter 40. Boost.Typeof</h2></div>
<div><div class="author"><h3 class="author">
<span class="firstname">Arkadiy</span> <span class="surname">Vertleyb</span>
</h3></div></div>
<div><div class="author"><h3 class="author">
<span class="firstname">Peder</span> <span class="surname">Holt</span>
</h3></div></div>
<div><p class="copyright">Copyright © 2004, 2005 Arkadiy Vertleyb, Peder Holt</p></div>
<div><div class="legalnotice">
<a name="typeof.legal"></a><p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">
        http://www.boost.org/LICENSE_1_0.txt </a>)
      </p>
</div></div>
</div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl class="toc">
<dt><span class="section"><a href="typeof.html#typeof.moti">Motivation</a></span></dt>
<dt><span class="section"><a href="typeof/tuto.html">Tutorial</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html">Reference</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="typeof/refe.html#typeof.auto">AUTO, AUTO_TPL</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.compl">COMPLIANT</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.incr">INCREMENT_REGISTRATION_GROUP</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.inte">INTEGRAL</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.limit_func">LIMIT_FUNCTION_ARITY</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.messages">MESSAGES</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.limit_size">LIMIT_SIZE</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.regtype">REGISTER_TYPE</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.regtemp">REGISTER_TEMPLATE</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.temp">TEMPLATE</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.typo">TYPEOF, TYPEOF_TPL</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.typn">TYPEOF_NESTED_TYPEDEF, TYPEOF_NESTED_TYPEDEF_TPL</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="typeof/other.html">Other considerations and tips</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="typeof/other.html#typeof.natem">Native typeof support and emulation</a></span></dt>
<dt><span class="section"><a href="typeof/other.html#typeof.parties">The three participating parties</a></span></dt>
<dt><span class="section"><a href="typeof/other.html#typeof.features">Supported features</a></span></dt>
<dt><span class="section"><a href="typeof/other.html#typeof.what">What needs to be registered?</a></span></dt>
<dt><span class="section"><a href="typeof/other.html#typeof.limi">Limitations</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="typeof/cont.html">Contributed By:</a></span></dt>
<dt><span class="section"><a href="typeof/ackn.html">Acknowledgements</a></span></dt>
</dl>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="typeof.moti"></a>Motivation</h2></div></div></div>
<p>
      Today many template libraries supply object generators to simplify object creation
      by utilizing the C++ template argument deduction facility. Consider <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span></code>.
      In order to instantiate this class template and create a temporary object of
      this instantiation, one has to supply template parameters, as well as parameters
      to the constructor:
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;(</span><span class="number">5</span><span class="special">,</span> <span class="number">3.14159</span><span class="special">);</span>
</pre>
<p>
      To avoid this duplication, STL supplies the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">make_pair</span></code>
      object generator. When it is used, the types of template parameters are deduced
      from supplied function arguments:
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">make_pair</span><span class="special">(</span><span class="number">5</span><span class="special">,</span> <span class="number">3.14159</span><span class="special">);</span>
</pre>
<p>
      For the temporary objects it is enough. However, when a named object needs
      to be allocated, the problem appears again:
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">p</span><span class="special">(</span><span class="number">5</span><span class="special">,</span> <span class="number">3.14159</span><span class="special">);</span>
</pre>
<p>
      The object generator no longer helps:
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_pair</span><span class="special">(</span><span class="number">5</span><span class="special">,</span> <span class="number">3.14159</span><span class="special">);</span>
</pre>
<p>
      It would be nice to deduce the type of the object (on the left) from the expression
      it is initialized with (on the right), but the current C++ syntax does not
      allow for this.
    </p>
<p>
      The above example demonstrates the essence of the problem but does not demonstrate
      its scale. Many libraries, especially expression template libraries, create
      objects of really complex types, and go a long way to hide this complexity
      behind object generators. Consider a nit Boost.Lambda functor:
    </p>
<pre class="programlisting"><span class="identifier">_1</span> <span class="special">&gt;</span> <span class="number">15</span> <span class="special">&amp;&amp;</span> <span class="identifier">_2</span> <span class="special">&lt;</span> <span class="number">20</span>
</pre>
<p>
      If one wanted to allocate a named copy of such an innocently looking functor,
      she would have to specify something like this:
    </p>
<pre class="programlisting"><span class="identifier">lambda_functor</span><span class="special">&lt;</span>
    <span class="identifier">lambda_functor_base</span><span class="special">&lt;</span>
        <span class="identifier">logical_action</span><span class="special">&lt;</span><span class="identifier">and_action</span><span class="special">&gt;,</span>
        <span class="identifier">tuple</span><span class="special">&lt;</span>
            <span class="identifier">lambda_functor</span><span class="special">&lt;</span>
                <span class="identifier">lambda_functor_base</span><span class="special">&lt;</span>
                    <span class="identifier">relational_action</span><span class="special">&lt;</span><span class="identifier">greater_action</span><span class="special">&gt;,</span>
                    <span class="identifier">tuple</span><span class="special">&lt;</span>
                        <span class="identifier">lambda_functor</span><span class="special">&lt;</span><span class="identifier">placeholder</span><span class="special">&lt;</span><span class="number">1</span><span class="special">&gt;</span> <span class="special">&gt;,</span>
                        <span class="keyword">int</span> <span class="keyword">const</span>
                    <span class="special">&gt;</span>
                <span class="special">&gt;</span>
            <span class="special">&gt;,</span>
            <span class="identifier">lambda_functor</span><span class="special">&lt;</span>
                <span class="identifier">lambda_functor_base</span><span class="special">&lt;</span>
                    <span class="identifier">relational_action</span><span class="special">&lt;</span><span class="identifier">less_action</span><span class="special">&gt;,</span>
                    <span class="identifier">tuple</span><span class="special">&lt;</span>
                        <span class="identifier">lambda_functor</span><span class="special">&lt;</span><span class="identifier">placeholder</span><span class="special">&lt;</span><span class="number">2</span><span class="special">&gt;</span> <span class="special">&gt;,</span>
                        <span class="keyword">int</span> <span class="keyword">const</span>
                    <span class="special">&gt;</span>
                <span class="special">&gt;</span>
            <span class="special">&gt;</span>
        <span class="special">&gt;</span>
    <span class="special">&gt;</span>
<span class="special">&gt;</span>
<span class="identifier">f</span> <span class="special">=</span> <span class="identifier">_1</span> <span class="special">&gt;</span> <span class="number">15</span> <span class="special">&amp;&amp;</span> <span class="identifier">_2</span> <span class="special">&lt;</span> <span class="number">20</span><span class="special">;</span>
</pre>
<p>
      Not exactly elegant. To solve this problem (as well as some other problems),
      the C++ standard committee is considering a few additions to the standard language,
      such as <code class="computeroutput"><span class="identifier">typeof</span><span class="special">/</span><span class="keyword">decltype</span></code> and <code class="computeroutput"><span class="keyword">auto</span></code>
      (see <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1607.pdf" target="_top">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1607.pdf</a>).
    </p>
<p>
      The <code class="computeroutput"><span class="identifier">typeof</span></code> operator (or <code class="computeroutput"><span class="keyword">decltype</span></code>, which is a slightly different flavor
      of <code class="computeroutput"><span class="identifier">typeof</span></code>) allows one to determine
      the type of an expression at compile time. Using <code class="computeroutput"><span class="identifier">typeof</span></code>,
      the above example can be simplified drastically:
    </p>
<pre class="programlisting"><span class="identifier">typeof</span><span class="special">(</span><span class="identifier">_1</span> <span class="special">&gt;</span> <span class="number">15</span> <span class="special">&amp;&amp;</span> <span class="identifier">_2</span> <span class="special">&lt;</span> <span class="number">20</span><span class="special">)</span> <span class="identifier">f</span> <span class="special">=</span> <span class="identifier">_1</span> <span class="special">&gt;</span> <span class="number">15</span> <span class="special">&amp;&amp;</span> <span class="identifier">_2</span> <span class="special">&lt;</span> <span class="number">20</span><span class="special">;</span>
</pre>
<p>
      Much better, but some duplication still exists. The <code class="computeroutput"><span class="keyword">auto</span></code>
      type solves the rest of the problem:
    </p>
<pre class="programlisting"><span class="keyword">auto</span> <span class="identifier">f</span> <span class="special">=</span> <span class="identifier">_1</span> <span class="special">&gt;</span> <span class="number">15</span> <span class="special">&amp;&amp;</span> <span class="identifier">_2</span> <span class="special">&lt;</span> <span class="number">20</span><span class="special">;</span>
</pre>
<p>
      The purpose of the Boost.Typeof library is to provide a library-based solution,
      which could be used until the language-based facility is added to the Standard
      and becomes widely available.
    </p>
</div>
</div>
<div class="copyright-footer"></div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_typeindex/acknowledgements.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="typeof/tuto.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
